﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Service;
using Model;
using Microsoft.Reporting.WebForms;


namespace ItaServicos.Controllers
{
    public class ServicoController : Controller
    {
        
        
        private GerenciadorServico gServico;
        private GerenciadorEspecialidade gEspecialidade;
     
        public ServicoController()
        {
            gServico = new GerenciadorServico();
            gEspecialidade = new GerenciadorEspecialidade();
        }
        
        //// GET: /Servico/

        public ActionResult Index()
        {
            
            return View(gServico.ObterTodos());
        }

        //// GET: /Servico/List/5
       
        [HttpGet]
        public ActionResult List()
        {
            
            return View();
        }

        //// GET: /Servico/Details/5
        
        [HttpGet]
        public ActionResult Details(int id)
        {
            ServicoModel servico = gServico.Obter(id);
            return View(servico);
            
        }

        //// GET: /Servico/Create

        [HttpGet]
        public ActionResult Create()
        {
            
            ViewBag.IDsespecialidade = new SelectList(gEspecialidade.ObterTodos(), "IDecodigo", "enome");
            return View();
            
        }

       
       

        //// POST: /Servico/Create

        [HttpPost]
        public ActionResult Create(ServicoModel servico)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    gServico.Inserir(servico);
                    return RedirectToAction("Index");
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //// GET: /Servico/Edit/5
        
        [HttpGet]
        public ActionResult Edit(int id)
        {
            
            ServicoModel servico = gServico.Obter(id);
            ViewBag.IDsespecialidade = new SelectList(gEspecialidade.ObterTodos(), "IDecodigo", "enome",servico.IDsespecialidade);
            return View(servico);
            
        }

        //// POST: /Servico/Edit/5

        [HttpPost]
        public ActionResult Edit( ServicoModel servico)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    gServico.Editar(servico);
                    return RedirectToAction("Index");
                }
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //// GET: /Servico/Delete/5
        
        [HttpGet]
        public ActionResult Delete( int id)
        {
            ServicoModel servico = gServico.Obter(id);
            return View(servico);
            
        }

        //// POST: /Servico/Delete/5

        [HttpPost]
        public ActionResult DeleteComfirmar(int id)
        {
            try
            {
                gServico.Remover(id);
                return RedirectToAction("Index");
 
             }
            catch
            {
                return View();
            }
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        public ActionResult RelatorioServico()
        {
            LocalReport relatorio = new LocalReport();

            //Caminho onde o arquivo do Report Viewer está localizado
            relatorio.ReportPath = Server.MapPath("~/Relatórios/servicos.rdlc");
            //Define o nome do nosso DataSource e qual rotina irá preenche-lo, no caso, nosso método criado anteriormente
            relatorio.DataSources.Add(new ReportDataSource("Servico", gServico.ObterTodos()));

            string reportType = "PDF";
            string mimeType;
            string encoding;
            string fileNameExtension;

            string deviceInfo =
             "<DeviceInfo>" +
             " <OutputFormat>PDF</OutputFormat>" +
             " <PageWidth>9in</PageWidth>" +
             " <PageHeight>11in</PageHeight>" +
             " <MarginTop>0.7in</MarginTop>" +
             " <MarginLeft>2in</MarginLeft>" +
             " <MarginRight>2in</MarginRight>" +
             " <MarginBottom>0.7in</MarginBottom>" +
             "</DeviceInfo>";

            Warning[] warnings;
            string[] streams;
            byte[] bytes;

            //Renderiza o relatório em bytes
            bytes = relatorio.Render(
            reportType,
            deviceInfo,
            out mimeType,
            out encoding,
            out fileNameExtension,
            out streams,
            out warnings);

            return File(bytes, mimeType);

        }
    
    
    
    
    
    
    
    }
}
